<?php
namespace addons\wanlshop\library\AliyunSdk;

final class Alilive 
{
	private $url_ssl;
	private $trans_template_switch; 
	private $auth_switch; 
	private $push_domain;
	private $push_key;
	private $appName;
	private $streamName;
	private $expireTime;
	private $play_domain;
	private $play_key;
	private $transTemplate;
	
	// 初始化
	public function __construct()
	{
		$config = get_addon_config('wanlshop');
		// 是否开启SSL证书
		$this->ssl_switch = $config['live']['sslSwitch'];
		// 是否开启转码
		$this->trans_template_switch = $config['live']['transTemplateSwitch'];
		// 是否开启鉴权
		$this->auth_switch = $config['live']['authSwitch'];
		//推流域名
		$this->push_domain = $config['live']['pushDomain'];
		//推流域名配置的鉴权Key
		$this->push_key = $config['live']['pushKey'];
		//生成随机的AppName
		$this->appName = $config['live']['appName'];
		//生成随机的streamName
		$this->streamName = md5(uniqid(microtime(true),true));
		//配置过期时间
		$this->expireTime = time() + (int)$config['live']['builderTime'] * 60;
		//播放域名
		$this->play_domain = $config['live']['liveDomain'];
		//播放域名配置的鉴权Key
		$this->play_key = $config['live']['liveKey'];
		// 播放转码模板
		$this->transTemplate = $config['live']['transTemplate'];
	}
	
    public function auth() 
	{
        $play_url = $this->play_url();
		return [
			'streamName' => $this->streamName, 
			'push_url' => $this->push_url(),
			'rtmp_play_url' => $play_url['rtmp_play_url'],
			'flv_play_url' => $play_url['flv_play_url'],
			'hls_play_url' => $play_url['hls_play_url']
		];
    }
	
    // 生成推流url
    private function push_url() 
	{
        $push_url = '';
        //未开启鉴权Key的情况下
        if ($this->auth_switch == 'N') {
            $push_url = 'rtmp://' . $this->push_domain . '/' . $this->appName . '/' . $this->streamName;
        }else{
			$sstring = '/' . $this->appName . '/' . $this->streamName . '-' . $this->expireTime . '-0-0-' . $this->push_key;
			$md5hash = md5($sstring);
			$push_url = 'rtmp://' . $this->push_domain . '/' . $this->appName . '/' . $this->streamName . '?auth_key=' . $this->expireTime . '-0-0-' . $md5hash;
		}
        return $push_url;
    }
	
    // 生成播流url
    private function play_url() 
	{
        // 判断转码模板是否存在
        if ($this->trans_template_switch == 'Y') {
            $streamName = $this->streamName .'_' . $this->transTemplate;
        }else{
			$streamName = $this->streamName;
		}
        //未开启鉴权Key的情况下
        if ($this->auth_switch == 'N') {
            // 判断是否存在转码模板，存在则输出转码模板流
            $rtmp_play_url = 'rtmp://' . $this->play_domain . '/' . $this->appName . '/' . $streamName;
            $flv_play_url = ($this->ssl_switch == 'Y' ? 'https://' : 'http://') . $this->play_domain . '/' . $this->appName . '/' . $streamName . '.flv';
            $hls_play_url = ($this->ssl_switch == 'Y' ? 'https://' : 'http://') . $this->play_domain . '/' . $this->appName . '/' . $streamName . '.m3u8';
        } else {
            $rtmp_sstring = '/' . $this->appName . '/' . $streamName . '-' . $this->expireTime . '-0-0-' . $this->play_key;
            $rtmp_md5hash = md5($rtmp_sstring);
            $rtmp_play_url = 'rtmp://' . $this->play_domain . '/' . $this->appName . '/' . $streamName . '?auth_key=' . $this->expireTime . '-0-0-' . $rtmp_md5hash;
           
			$flv_sstring = '/' . $this->appName . '/' . $streamName . '.flv-' . $this->expireTime . '-0-0-' . $this->play_key;
            $flv_md5hash = md5($flv_sstring);
            $flv_play_url = ($this->ssl_switch == 'Y' ? 'https://' : 'http://') . $this->play_domain . '/' . $this->appName . '/' . $streamName . '.flv?auth_key=' . $this->expireTime . '-0-0-' . $flv_md5hash;
           
			$hls_sstring = '/' . $this->appName . '/' . $streamName . '.m3u8-' . $this->expireTime . '-0-0-' . $this->play_key;
            $hls_md5hash = md5($hls_sstring);
            $hls_play_url = ($this->ssl_switch == 'Y' ? 'https://' : 'http://') . $this->play_domain . '/' . $this->appName . '/' . $streamName . '.m3u8?auth_key=' . $this->expireTime . '-0-0-' . $hls_md5hash;
        }
        return ['rtmp_play_url' => $rtmp_play_url, 'flv_play_url' => $flv_play_url, 'hls_play_url' => $hls_play_url];
    }
}

